package com.tul.exercise.base.clazz.clazzmethod;


import org.junit.jupiter.api.Test;

/**
 * 《java编程思想》14.2 示例1
 *  class的方法
 */
public class ToyTest {

    static void printInfo(Class cc) {
        System.out.println("Class name: " + cc.getName() + ",is interface? [" + cc.isInterface() + "]");
        System.out.println("Simple name: " + cc.getSimpleName());
        System.out.println("Canonical name: " + cc.getCanonicalName());
        System.out.println("\n\n");
    }

    public static void main(String[] args) {
        Class c = null;

        try {
            c = Class.forName("com.tul.exercise.base.clazz.clazzmethod.FancyToy");

        } catch (ClassNotFoundException e) {
            System.out.println("Can't find class");
            System.exit(1);
        }
        printInfo(c);

        for(Class face :c.getInterfaces()) {
            printInfo(face);
        }

        Class up = c.getSuperclass();
        Object obj = null;

        try {
            obj = up.newInstance();
        }catch ( InstantiationException e) {
            System.out.println("Can't  instantiate");
        } catch(IllegalAccessException e) {
            System.out.println("Can't access");
            System.exit(1);
        }
        printInfo(obj.getClass());
    }

    /**
     * 递归打印类的基类
     * @param obj 类
     */
    public static void printSuperClass(Object obj) {
        Class cc = obj.getClass().getSuperclass();
        System.out.println(cc);
        //cc.


        if(!cc.getSimpleName().equals("Object")) {
            try {
                printSuperClass(cc.newInstance());
            }catch (InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    public static boolean isBaseClass(Object obj) {
        boolean reuslt = false;

        return reuslt;
    }

    @Test
    public void printSuperClass() {
        FancyToy toy = new FancyToy();

        printSuperClass(toy);

    }
}
